import { Note } from '../../modle/Note'

const fs = require('fs')
const path = require('path')

// const { Note } = require('../model/note')

function sortFn (a: Note, b: Note) {
  if (a.isDir) return 0
  else return 1
}

export function getNotes (p:string) {
  let files = fs.readdirSync(p)
  let notes = []
  for (let file of files) {
    if (file === '.images') {
      continue
    }
    let stat = fs.statSync(path.join(p, file))

    let note = new Note(p, file, stat.isDirectory(path.join(p, file)), [])
    if (note.isDir) {
      note.children = getNotes(path.join(p, note.name))
      note.children.sort(sortFn)
    }
    notes.push(note)
  }
  notes.sort(sortFn)
  return notes
}
